package com.china08.yxyapi.api.manage.tec.teaching.weikecheng.upload;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResVideoBlock;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResVideoBlockRepository;
import com.china08.yxyapi.util.YxyResUtil;

/**
 * 上传片段文件
 * 
 * @author Json
 *
 */
@CrossOrigin
@RestController
public class WeikechengUploadBlockController extends YxyApiBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(WeikechengUploadBlockController.class);

    @Autowired
    private YxyResVideoBlockRepository yxyResVideoBlockRepository;

    @Value("${weike.storage.upload.video}")
    private String uploadBasePath;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/weikecheng/upload/block", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestPart MultipartFile file,
            @RequestParam(value = "md5", required = true) String md5, // 整个文件的md5
            @RequestParam(value = "chunk", required = false, defaultValue = "0") Integer chunk, // 当前第几片
            @RequestParam(value = "chunks", required = false, defaultValue = "1") Integer chunks, // 一共分了多少片
            @RequestParam(value = "size") Integer size // 文件大小
    ) {
        RespModel respModel = new RespModel();
        try { // 临时文件夹 格式 /mnt/gfs/YxyRes/upload/temp/2018/01/md5/
            String savePath = uploadBasePath + "/temp" + YxyResUtil.getPathMonth() + File.separator + md5;
            File sf = new File(savePath);
            String realPath = sf.getPath() + File.separator + chunk;
            FileUtils.forceMkdir(sf);
            FileUtils.writeByteArrayToFile(new File(realPath), file.getBytes());
            YxyResVideoBlock yxyResVideoBlock = new YxyResVideoBlock();
            yxyResVideoBlock.setMd5(md5);
            yxyResVideoBlock.setChunk(chunk);
            yxyResVideoBlock.setChunks(chunks);
            yxyResVideoBlock.setFlag(false);
            yxyResVideoBlockRepository.save(yxyResVideoBlock);
            respModel.status = 1;
        } catch (Exception e) {
            LOGGER.error("上传文件片段异常:" + e);
            LOGGER.error(e.getMessage());
            e.printStackTrace();
        }
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 0 失败 1成功
        public Integer status = 0;
    }

}